package cn.edu.ujn.liuklab3.service;

import cn.edu.ujn.liuklab3.entity.Review;
import cn.edu.ujn.liuklab3.dao.ReviewDao;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("reviewService")
public class ReviewServiceImpl implements ReviewService {
    @Resource
    private ReviewDao reviewDao;

    @Override
    public Review queryById(Integer reviewId) {
        return this.reviewDao.queryById(reviewId);
    }

    @Override
    public Page<Review> queryByPage(Review review, PageRequest pageRequest) {
        long total = this.reviewDao.count(review);
        return new PageImpl<>(this.reviewDao.queryAllByLimit(review, pageRequest), pageRequest, total);
    }

    @Override
    public Review insert(Review review) {
        this.reviewDao.insert(review);
        return review;
    }

    @Override
    public Review update(Review review) {
        this.reviewDao.update(review);
        return this.queryById(review.getReviewId());
    }

    @Override
    public boolean deleteById(Integer reviewId) {
        return this.reviewDao.deleteById(reviewId) > 0;
    }

    @Override
    public cn.edu.ujn.util.Page<Review> selectAll(Review review) {
        List<Review> reviews = reviewDao.selectAll(review);
        cn.edu.ujn.util.Page<Review> page = new cn.edu.ujn.util.Page<>();
        page.setPage(review.getStart());
        page.setRows(reviews);
        page.setSize(review.getRows());
        page.setTotal(reviewDao.selectCount());
        return page;
    }
}
